Python uiautomation操作微信模拟点击 您所在的位置:网站首页 python pywinauto 微信好友界面Edit3详解 Python uiautomation操作微信模拟点击

Python uiautomation操作微信模拟点击

2024-05-31 05:18| 来源: 网络整理| 查看: 265

Python uiautomation

Python uiautomation 是一个用于自动化 GUI 测试和操作的库,它可以模拟用户操作来执行各种任务。通过这个库,可以使用Python脚本模拟人工点击,人工操作界面。本文使用 Python uiautomation 进行微信电脑版的操作。

微信电脑版

以下是本次实验的版本号。

在这里插入图片描述

你需要安装 uiautomation

pip install uiautomation

然后使用 uiautomation 进行操作。

代码 import time import uiautomation as auto import re from plyer import notification notification_history = {} # 历史消息 def check_wechat_messages(): # 获取微信窗口 wechat_win = auto.WindowControl(Name="微信", ClassName="WeChatMainWndForPC") shoukuanWin = wechat_win.ListControl(Name="会话") bbb = shoukuanWin.GetChildren() for chatMsg in bbb: if "条新消息" in chatMsg.Name: # 计算消息条数 match = re.match(r'([a-zA-Z0-9]+)(\d+)条新消息', chatMsg.Name) if match: nickname = match.group(1) message_count = int(match.group(2)) printInfo = f"{nickname} 给你发送了 {message_count} 条消息" print(printInfo) print("------------") # 获取消息列表控件 xiaoxis = wechat_win.ListControl(Name="消息") # 获取消息列表控件的子控件 xiaoxi_children = xiaoxis.GetChildren() # 获取最后一个子控件 last_xiaoxi = xiaoxi_children[-1] # 打印最后一条消息的内容 print(last_xiaoxi.Name) # 在指定时间内不重发 last_notification_time = notification_history.get((nickname, message_count), 0) current_time = time.time() if current_time - last_notification_time > 15: # 依次发送 notification_title = f"来自 {nickname} 的 {message_count} 条消息" notification_message = f"{last_xiaoxi.Name}" notification.notify( title=notification_title, message=notification_message, app_name="WeChat" ) # 更新日志 notification_history[(nickname, message_count)] = current_time if __name__ == "__main__": try: while True: check_wechat_messages() time.sleep(2) #2秒检测一次UI组件 except KeyboardInterrupt: print("程序退出~") except Exception as e: print(f"程序执行出现了问题: {str(e)}")

代码解析:

以上代码使用 uiautomation 实时获取微信聊天列表的消息状态,一旦有消息发过来,就会获取到发送人的微信昵称以及发送的消息内容、消息个数。

在这里插入图片描述

每2秒获取一次UI控件的内容,实测挂在后台对CPU和内存占用并无明显影响。

结合Python uiautomation的各种用法,可以做成自动回复的功能。

UISpy.exe

使用这款软件,可以获取到微信电脑版大部分控件的内容。

例如:微信聊天列表、群名称、好友微信昵称、群人数、微信号等。

在这里插入图片描述

还可以获取到群内的每一条聊天内容,获取到你跟好友的聊天记录。

在这里插入图片描述

只要 UISpy.exe 可获取到的控件内容,那么你用 Python就可以获取到。

作者

TANKING



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有